home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Frameworks / ShowcaseApp 1.2a / CSApplication.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-21  |  17.0 KB  |  620 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.  
  3.  CSApplication.c
  4.  
  5.     A Network group project, brought to you by the good folks at TCL-Talk and
  6.     comp.sys.mac.oop.tcl.
  7.     
  8.     
  9.     DOCUMENTATION
  10.     
  11.     An application from which example classes can be launched.  Designed to allow 
  12.     simple addition of example classes and a demo area using resources.
  13.     
  14.     Suggestion: Create a custom Director, make a window from the Init code, make
  15.     an instance or more of your new class. Show off!
  16.     
  17.         
  18.     APPLICATION INFO
  19.  
  20.     This app is based on the generic starter application supplied with Think 6.0. 
  21.     Current revisions as of the last compile are:
  22.     
  23.             THINK C                 6.0.1        
  24.             TCL                        1.1.3
  25.  
  26.  
  27.     ADDING NEW DEMOS
  28.             
  29.     Step 1) Add all necessary source and resource files to this project in a 
  30.             new segment.
  31.     
  32.     Step 2) Follow any additional instructions in the Read Me file (such as
  33.             adding non-core TCL classes that aren't already in the project).
  34.  
  35.     Step 3) Compile the Showcase Application. During compilation, the compiler
  36.             may complain about duplicate EXCs resources. Since the ID is not
  37.             significant, just use ResEdit to change the EXCs to a random number
  38.             and then try compiling again. 
  39.             
  40.             NOTE: THINK 5 allowed only one .rsrc file per project; you had 
  41.             the merge all the resources of TCL and your application into this
  42.             file. TPM 6, however, allows many resource files and it will build
  43.             the project's resource file for you. Because of this, old Showcase
  44.             Application demos will give other duplicate resource errors because
  45.             they often have the demo's, Showcase's, and TCL's resources all in
  46.             the demo's resource file. To fix this, remove from the demo's 
  47.             resource file all the non-EXCs duplicate reosurces. When all else
  48.             fails, build just one Showcase EXCs at a time.
  49.  
  50.     Step 4) Run the Showcase Application and enjoy the show!
  51.     
  52.     NOTE: If you want to build the application, see the note for the 
  53.     ForceClassReferences method.
  54.     
  55.         
  56.     CREATE NEW DEMOS
  57.     
  58.     To create a demo for your class, you must subclass the CShowcaseDemoDir class 
  59.     and then override the INewDemo method.  See the end of the CSApplication.h 
  60.     header file for the CShowcaseDemoDir class. Name your subclass something like 
  61.     "CNewClassDemoDir" where NewClass is the name of the class you are demoing. 
  62.     Your subclass should create a window (or whatever) in which to demo your new 
  63.     class.
  64.     
  65.     Next, create a resource file and add an EXCs resource for your demo.
  66.     It has the following format (there is a TMPL for the EXCs included in
  67.     CSApplication.rsrc)
  68.     
  69.         PSTR    DemoDirector Class Name
  70.                         Name of your CShowcaseDemoDir subclass
  71.         PSTR    Class Name
  72.                         Name of the class you are demoing
  73.         PSTR    Class Description
  74.                         Brief description of the class you are demoing
  75.         PSTR    Author Name
  76.                         Name of the class's author
  77.         PSTR    Author Address
  78.                         E-Mail address of the author
  79.     
  80.     Finally, add any other support resources necessary to make your class work.
  81.     Include all the classes required for your class (except those that are
  82.     already in the THINK Class Library).
  83.     
  84.     Take a look at the examples already on the archive to see how it's done.
  85.     This was made as simple as possible, so it should only take a few minutes
  86.     to put something basic together, but don't be afraid to show off a little! :)
  87.  
  88.     NOTE: Use obscure IDs for your resources.
  89.  
  90.  
  91.     WORKER BEES
  92.     
  93.     Brian Hamlin
  94.     bhamlin@netcom.com
  95.     
  96.     David Harkness
  97.     harkness@netcom.com
  98.  
  99.     Andrew Gilmartin
  100.     Andrew_Gilmartin@Brown.Edu
  101.  
  102.     
  103.     VERSION HISTORY
  104.     
  105.     1.2a    93-10-21    ajg
  106.     -----------------------
  107.     Modified project and source to work with THINK C 6.0 and TCL 1.1.3. I have not
  108.         tried using it with a SC++ demo class. Very minor testing of changes!
  109.     Updated the documentation.
  110.  
  111.     1.1a    92-06-06    djh
  112.     -----------------------
  113.     Fixed some bugs
  114.     Added CShowcaseDemoDir for creating demos
  115.     Removed demo command number method and replaced with new_by_name
  116.     Added the Showcase menu (I'm too lazy to double-click in a list ;)
  117.     Modified the NewClassDemo window a little
  118.     Wrote the "Name/Author/Address Extraction" routines
  119.         (Hey, do I still get the wonderful no-prize?)
  120.     
  121.     1.0a    92-05-08    bh
  122.     ----------------------
  123.     Created the NewClassDemo window
  124.     Layed the foundation and got the project running
  125.     Hmm... Since I wasn't there when he wrote his code, how am I supposed to
  126.         comment on what revisions he made??
  127.  
  128.     COPYRIGHT
  129.     
  130.     Copyright (C) 1992, 1993 by TCL-Talk & comp.sys.mac.oop.tcl.
  131.     
  132.     Permission to use, copy, modify, and distribute this software and its
  133.     documentation for any purpose and without fee is hereby granted, provided
  134.     that the above copyright notice appear in all copies and that both that
  135.     copyright notice and this permission notice appear in supporting
  136.     documentation.  This software is provided "as is" without express or
  137.     implied warranty.
  138.  
  139. ******************************************************************************/
  140.  
  141. #include <CDecorator.h>
  142. #include <CWindow.h>
  143. #include <CScrollPane.h>
  144. #include <CPicture.h>
  145. #include <CEditText.h>
  146. #include <CPaneBorder.h>
  147. #include <CBartender.h>
  148. #include <Global.h>
  149. #include <Commands.h>
  150. #include "CSApplication.h"
  151.  
  152.  
  153. /******************************************************************************
  154.     CSHOWCASEDEMODIR SUBCLASS #INCLUDE FILES, IF NECESSARY   (see note above)
  155. ******************************************************************************/
  156.  
  157.  
  158.  
  159.  
  160. /******************************************************************************
  161.     GLOBALS
  162. ******************************************************************************/
  163.  
  164. extern CApplication    *gApplication;
  165. extern CBartender    *gBartender;
  166. extern CDesktop        *gDesktop;
  167. extern CDecorator    *gDecorator;
  168. extern CursHandle gWatchCursor;
  169.  
  170. Handle        *demoClasses;
  171. short        numDemos;
  172.  
  173.  
  174. /******************************************************************************
  175.     MAIN
  176. ******************************************************************************/
  177. void main()
  178.  
  179. {
  180.     CSApplication    *Application;                    
  181.  
  182.     Application = new CSApplication;
  183.     
  184.     Application->ISApplication();
  185.     Application->Run();
  186.     Application->Exit();
  187. }
  188.  
  189.  
  190. /******************************************************************************
  191.     App::IApplication
  192. ******************************************************************************/
  193. void CSApplication::ISApplication(void)
  194. {
  195.     CApplication::IApplication( kExtraMasters, kRainyDayFund, 
  196.                                 kCriticalBalance, kToolboxBalance);
  197.     
  198.     itsNewDocWindow = NULL;
  199. }
  200.  
  201.  
  202. /******************************************************************************
  203.     App::CreateDocument
  204. ******************************************************************************/
  205. void CSApplication::CreateDocument(void)
  206. {
  207.     if ( itsNewDocWindow )
  208.     {
  209.         itsNewDocWindow->GetWindow()->Select();
  210.         return;
  211.     }
  212.     
  213.     TRY
  214.     {
  215.         itsNewDocWindow = new( CSDirector);
  216.         itsNewDocWindow->ISDirector();
  217.     }
  218.     CATCH
  219.     {
  220.         ForgetObject( itsNewDocWindow);
  221.     }
  222.     ENDTRY
  223. }
  224.  
  225.  
  226. /******************************************************************************
  227.     App::RemoveDirector
  228. ******************************************************************************/
  229. void CSApplication::RemoveDirector( CDirector *aDirector)
  230. {
  231.     if ( aDirector == itsNewDocWindow )
  232.         itsNewDocWindow = NULL;
  233.     
  234.     inherited::RemoveDirector( aDirector);
  235. }
  236.  
  237.  
  238. /******************************************************************************
  239.     App::SetUpMenus
  240. ******************************************************************************/
  241. void CSApplication::SetUpMenus()
  242. {
  243.     Handle            rH;
  244.     short            index;
  245.     unsigned char    *cp;
  246.     
  247.     inherited::SetUpMenus();
  248.     gBartender->SetDimOption( kMENUShowcase, dimNONE);
  249.         
  250.     numDemos = Count1Resources( kInfoResType);
  251.     
  252.     if ( numDemos < 1 )
  253.         ExitToShell();
  254.     
  255.     demoClasses = (Handle *)NewPtr( sizeof(Handle) * numDemos);
  256.     
  257.     if ( demoClasses )
  258.         for ( index = 1; index <= numDemos; index++ )
  259.         {
  260.             rH = GetIndResource( kInfoResType, index);
  261.             demoClasses[index-1] = rH;                        // Store handle
  262.             HLock( rH);
  263.             cp = (unsigned char *)*rH;
  264.             cp += *cp + 1;
  265.                                                             // Place in menu
  266.             gBartender->InsertMenuCmd( cmdOpenShowcase+index, cp,
  267.                                        kMENUShowcase, index-1);
  268.             HUnlock( rH);
  269.         }
  270.     else
  271.         ExitToShell();
  272. }
  273.  
  274.  
  275. /******************************************************************************
  276.     App::DoCommand
  277. ******************************************************************************/
  278. void CSApplication::DoCommand(long theCommand)
  279. {
  280.     CShowcaseDemoDir    *demoDir;
  281.     Handle                rH;
  282.     unsigned char        *cp;
  283.     char                cstr[256];
  284.     short                index;
  285.     
  286.     if ( theCommand > cmdOpenShowcase && theCommand <= cmdLastShowcase )
  287.     {
  288.         SetCursor( *gWatchCursor );
  289.  
  290.         index = theCommand - cmdOpenShowcase;
  291.         
  292.         rH = demoClasses[index-1];
  293.         LoadResource( rH);                            // Make sure it's there!
  294.         HLock( rH);
  295.         
  296.         cp = (unsigned char *)*rH;                    // Reference to class name
  297.         BlockMove( cp+1, cstr, cp[0]);
  298.         cstr[cp[0]] = '\0';
  299.         demoDir = new_by_name( cstr);
  300.         if ( !demoDir )
  301.             Failure( paramErr, excNewByNameFailed);
  302.         
  303.         HUnlock( rH);
  304.         ASSERT( member( demoDir, CShowcaseDemoDir));        // Just to be sure
  305.         
  306.         demoDir->INewDemo( this);
  307.     }
  308.     else
  309.         inherited::DoCommand( theCommand);
  310. }
  311.  
  312.  
  313. /******************************************************************************
  314.     App::UpdateMenus
  315. ******************************************************************************/
  316. void CSApplication::UpdateMenus()
  317. {
  318.     inherited::UpdateMenus();
  319. }
  320.  
  321.  
  322. /******************************************************************************
  323.     App::ForceClassReferences
  324.     
  325.         You only need to add a dummy reference for each CShowcaseDemoDir
  326.         _if_ you build an application.  The classes will be stripped by
  327.         the Smart Linker, but not by the compiler alone.
  328.         Of course, if you do this, don't forget to #include their respective
  329.         header files at the top of this source file.
  330. ******************************************************************************/
  331. void CSApplication::ForceClassReferences( void)
  332. {
  333.                 // All CShowcaseDemoDir objects are created with new_by_name
  334.     Boolean    alwaysFalse = FALSE;
  335.     CObject *dummy;
  336.     
  337.     if (alwaysFalse)
  338.     {
  339.         member( dummy, CShowcaseDemoDir);                // E X A M P L E
  340.         
  341.         
  342.     }
  343.     
  344.     inherited::ForceClassReferences();
  345. }
  346.  
  347.  
  348. /******************************************************************************
  349.     App::Exit
  350. ******************************************************************************/
  351. void CSApplication::Exit()
  352. {
  353.     
  354. }
  355.  
  356.  
  357.  
  358. //*****************************************************************************
  359.                                                         //  D I R E C T O R
  360.  
  361.  
  362. /******************************************************************************
  363.     Dir::ISDirector
  364. ******************************************************************************/
  365. void CSDirector::ISDirector()
  366. {
  367.     inherited::IDirector( gApplication);
  368.  
  369.     itsWindow = new(CWindow);
  370.     itsWindow->IWindow( 500, FALSE, gDesktop, this);
  371.     gDecorator->CenterWindow(itsWindow);
  372.  
  373.     itsPane = new( CSPane);
  374.     itsPane->ISPane( itsWindow, this );
  375.     itsWindow->Select();
  376. }
  377.  
  378.  
  379. /******************************************************************************
  380.     Dir::Dispose
  381. ******************************************************************************/
  382. void CSDirector::Dispose()
  383. {
  384.   inherited::Dispose();       
  385. }
  386.  
  387.  
  388. /******************************************************************************
  389.     Dir::UpdateMenus
  390. ******************************************************************************/
  391. void CSDirector::UpdateMenus()
  392. {
  393.   inherited::UpdateMenus();       
  394. }
  395.  
  396.  
  397. /******************************************************************************
  398.     Dir::DoCommand
  399. ******************************************************************************/
  400. void CSDirector::DoCommand(long theCommand)
  401. {
  402.     switch (theCommand) {
  403.         
  404.         default:    
  405.             inherited::DoCommand(theCommand);
  406.             break;
  407.     }
  408. }
  409.  
  410.  
  411. //*****************************************************************************
  412.                                                         //  P A N E
  413.  
  414.  
  415. /******************************************************************************
  416.     Pane::ISPane
  417. ******************************************************************************/
  418. void CSPane::ISPane( CView *anEnclosure, CBureaucrat *aSupervisor)
  419. {
  420.     CPicture            *aPic;
  421.     CEditText            *anEditText;
  422.     CPaneBorder            *aPB;
  423.     CScrollPane            *aScrollPane;
  424.     CSTable                *anSTable;
  425.     StringHandle        rStr;
  426.     Rect                margin;
  427.     
  428.     CPane::IPane( anEnclosure, aSupervisor, 0, 0, 0, 0,
  429.                   sizFIXEDLEFT, sizFIXEDTOP);
  430.  
  431.     this->FitToEnclFrame( true, true);
  432.     this->SetWantsClicks( true);
  433.  
  434.                                                     // Make a Picture
  435.     aPic = new( CPicture);
  436.     aPic->IPicture( this, aSupervisor,
  437.                     this->frame.right, this->frame.bottom, 0, 0,
  438.                     sizFIXEDLEFT, sizFIXEDTOP);
  439.     aPic->UsePICT( 1024);
  440.     aPic->FrameToBounds();
  441.     aPic->Place( 2, 2, false);
  442.     
  443.                                                     // Make a Static Text
  444.     anEditText = new( CEditText);
  445.     anEditText->IEditText( this, aSupervisor,
  446.                     (this->frame.right/3)*2 - 40, this->frame.bottom/3 + 22,
  447.                     8, (this->frame.bottom/3)*2 - 24,
  448.                     sizFIXEDLEFT, sizFIXEDTOP,
  449.                     (this->frame.right/3)*2 );
  450.     rStr = GetString( 1024);
  451.     HLock( (Handle) rStr);
  452.     anEditText->SetTextPtr( (Ptr)((char *)*rStr+1), *(unsigned char *)*rStr);
  453.     anEditText->Specify( false, false, false);
  454.     HUnlock( (Handle) rStr);
  455.     
  456.     aPB = new( CPaneBorder);
  457.     aPB->IPaneBorder( kBorderFrame);
  458.     SetRect( &margin, -2, 0, 2, 0);
  459.     aPB->SetPenSize( 2, 2);
  460.     aPB->SetMargin( &margin);
  461.     anEditText->SetBorder( aPB);
  462.     
  463.     anEditText->SetFontSize( 9);
  464.     
  465.                                                     // Make a CSTable
  466.     aScrollPane = new( CScrollPane);
  467.     aScrollPane->IScrollPane(  this, aSupervisor,
  468.                     this->frame.right/3 + 26, this->frame.bottom,
  469.                     (this->frame.right/3)*2 - 25, 0,
  470.                     sizFIXEDLEFT, sizFIXEDTOP,
  471.                     false, true, false);
  472.     aScrollPane->SetWantsClicks( true);
  473.         
  474.     anSTable = new( CSTable);
  475.     anSTable->ISTable( aScrollPane, aSupervisor,
  476.                     aScrollPane->width - 16, 0, 0, 0);
  477.     anSTable->FitToEnclosure( TRUE, TRUE);
  478.  
  479.     aPB = new( CPaneBorder);                        // re-use ptr, new object
  480.     aPB->IPaneBorder( kBorderFrame);
  481.     anSTable->SetBorder( aPB);
  482.         
  483.     aScrollPane->InstallPanorama( anSTable);
  484. }
  485.  
  486.  
  487. /******************************************************************************
  488.     Pane::Dispose
  489. ******************************************************************************/
  490. void CSPane::Dispose()
  491. {
  492.   inherited::Dispose();       
  493. }
  494.  
  495.  
  496. //*****************************************************************************
  497.                                                         //  T A B L E
  498.  
  499.  
  500. /******************************************************************************
  501.     Table::ISTable
  502. ******************************************************************************/
  503. void CSTable::ISTable( CView *anEnclosure, CBureaucrat *aSupervisor,
  504.                 short aWidth, short aHeight,
  505.                 short aHEncl, short aVEncl)
  506. {
  507.     
  508.     CTable::ITable( anEnclosure,  aSupervisor,
  509.                 aWidth, aHeight, aHEncl, aVEncl,
  510.                 sizFIXEDLEFT, sizFIXEDTOP);
  511.     
  512.     this->SetDefaults( aWidth, 60);
  513.     this->AddCol( 1, 0);
  514.         
  515.     this->AddRow( numDemos, 0);
  516.     
  517.     this->SetDblClickCmd( cmdOpenShowcase);
  518.     this->SetSelectionFlags( selOnlyOne);    
  519. }
  520.  
  521. /******************************************************************************
  522.     Table::Dispose
  523. ******************************************************************************/
  524. void CSTable::Dispose()
  525. {
  526.     inherited::Dispose();       
  527. }
  528.  
  529.  
  530. /******************************************************************************
  531.     Table::DoCommand
  532. ******************************************************************************/
  533. void CSTable::DoCommand( long aCmd)
  534. {
  535.     Cell            aCell;
  536.     
  537.     switch (aCmd)
  538.     {
  539.         case  cmdOpenShowcase:
  540.             SetCell( aCell, 0, 0);
  541.             if ( GetSelect( true, &aCell) )
  542.                 DoCommand( cmdOpenShowcase + aCell.v + 1);
  543.             break;
  544.         
  545.         default:
  546.             inherited::DoCommand( aCmd);
  547.             break;
  548.     }
  549. }
  550.  
  551.  
  552. /******************************************************************************
  553.     Table::DrawCell
  554. ******************************************************************************/
  555. void CSTable::DrawCell( Cell theCell, Rect *cellRect)
  556. {
  557.     Rect            r;
  558.     Handle            rH;
  559.     unsigned char    *cp;
  560.     
  561.     r = *cellRect;
  562.     InsetRect( &r, 2, 2);
  563.     r.right -= 1;
  564.     FrameRect( &r);
  565.     
  566.     TextSize( 9);
  567.     TextFont( geneva);
  568.     TextFace( bold);
  569.     
  570.     rH = demoClasses[theCell.v];
  571.     LoadResource( rH);                                // Make sure it's there!
  572.     HLock( rH);
  573.     cp = (unsigned char *)*rH;                        // Dereference
  574.     cp += *cp + 1;                                    // Jump over DemoDir name
  575.     
  576.     MoveTo( r.left+3, r.top+10-0);            // N A M E
  577.     DrawString( cp);
  578.     cp += *cp + 1;                                    // Jump over name
  579.     
  580.     TextFace( 0);
  581.     
  582.     MoveTo( r.left+3, r.top+21-0);            // D E S C R I P T I O N
  583.     DrawString( cp);
  584.     cp += *cp + 1;                                    // Jump over description
  585.     
  586.     MoveTo( r.left+3, r.top+40-1);            // A U T H O R
  587.     DrawString( cp);
  588.     cp += *cp + 1;                                    // Jump over author
  589.     
  590.     MoveTo( r.left+3, r.top+51);            // A D D R E S S
  591.     DrawString( cp);
  592.     HUnlock( rH);
  593. }
  594.  
  595.  
  596.  
  597. //*****************************************************************************
  598.                                                 //  D E M O   D I R E C T O R
  599.  
  600.  
  601. /******************************************************************************
  602.     DemoDir::INewDemo
  603.     
  604.         To create your own DemoDirector, subclass CShowcaseDemoDir and
  605.         override this method.  Your method should call this method first
  606.         and then perform any initialization necessary (open a window,
  607.         create your panes, whatever) and then start the demo.
  608. ******************************************************************************/
  609. void CShowcaseDemoDir::INewDemo( CDirectorOwner *aSupervisor)
  610. {
  611.     inherited::IDirector( aSupervisor);
  612. }
  613.  
  614.  
  615.  
  616. //*****************************************************************************
  617.                                                 // E N D   O F   L I S T I N G 
  618.  
  619.  
  620.